package com.ld.security.server.user.modules.ups.service.impl;


import com.hankcs.hanlp.mining.cluster.ClusterAnalyzer;
import com.ld.security.common.mybatis.base.service.*;
import com.ld.security.server.user.modules.ups.constant.UpsUserLikeConstant;
import com.ld.security.server.user.modules.ups.entity.UpsKeywordEntity;
import com.ld.security.server.user.modules.ups.mapping.UpsKeywordMapping;
import com.ld.security.server.user.modules.ups.service.IUpsUserLikeService;

import com.ld.security.server.user.modules.ups.entity.UpsUserLikeEntity;

import com.ld.security.server.user.modules.ups.service.IUpsUserLikeService;

import com.ld.security.server.user.modules.ups.vo.KeywordLikeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

@Service
@Transactional
public class UpsUserLikeServiceImpl  extends ISuperServiceImpl<UpsUserLikeEntity>
implements
        IUpsUserLikeService {


        @Autowired
        UpsKeywordMapping upsKeywordMapping;


        private KeywordLikeVo getKeywordLikeVo(String upsUserId){
                KeywordLikeVo vo = new KeywordLikeVo();
                vo.setUpsUserId(upsUserId);
                this.proxyQuery(vo);
                return vo;
        }

        @Override
        public void buildUserLike(String upsUserId) {
                KeywordLikeVo vo =  this.getKeywordLikeVo(upsUserId);
                ClusterAnalyzer<String> analyzer = new ClusterAnalyzer<String>();
                Map<String,List<UpsKeywordEntity>>  valMap = vo.getLikeUserMap();
                valMap.forEach((x,v) -> {
                        List<String> keys =  v.stream().map(val -> val.getKeyword()).collect(Collectors.toList());
                        analyzer.addDocument(x,keys);
                });
                List<Set<String>> list =  analyzer.repeatedBisection(1.0);

                        //便利所有兴趣组
                for (Set<String> strings : list) {
                        //判断用户是否属于这个兴趣组
                        if(strings.contains(upsUserId)){
                                for (String likeUser : strings) {
                                        if(!likeUser.equals(upsUserId)){
                                                UpsUserLikeEntity like =  new UpsUserLikeEntity();
                                                like.setUpsUserId(upsUserId);
                                                like.setUpsUserLikeId(likeUser);
                                                like.setWtType(UpsUserLikeConstant.wtType.like_user);
                                                vo.addLikeUser(like);
                                        }
                                }
                        }
                }
                this.proxySave(vo);
            

        }
}
